﻿using Aurora.TaskScheduler.Extension;
using Aurora.TaskScheduler.Storage.MySql;
using Volo.Abp;
using Volo.Abp.Modularity;
using Aurora.TaskScheduler.DistributedLock;

namespace Gateway.UI.Api.AbpModules
{
    /// <summary>
    /// 
    /// </summary>
    [DependsOn()]
    public class TaskSchedulerModule : AbpModule
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task ConfigureServicesAsync(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();

            #region 注入  scheduler

            context.Services.AddTaskScheduler(options =>
            {
                options.DefaultGroupName = "GATEWAY";
                options.RetentionTime = 2;
                options.UseMySql(x => x.ConnectionString = configuration["ConnectionSetting"]!);
                // options.AddQuartzUI(x =>
                // {
                //     x.PathBase = "/QuartzUI";
                //     x.DefaultPassword = "123456";
                //     x.DefaultUserName = "admin";
                // });
                options.UseTaskLock = true;
                options.UseRedisTaskDistribute(redisLockOptions =>
                {
                    redisLockOptions.ConnectionString = configuration["TaskScheduleRedisLockConnectionString"]!;
                });
            });

            #endregion

            return base.ConfigureServicesAsync(context);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            //开启task scheduler
            app.UseTaskScheduler(); // application => application.UseQuartzUI()
            return base.OnApplicationInitializationAsync(context);
        }
    }
}